package com.jzt.qf.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.jzt.qf.beans.HttpResult;
import com.jzt.qf.beans.UploadBase;
import com.jzt.qf.beans.jztBean.*;
import com.jzt.qf.beans.qfBean.ZxyUploadbillJzt;
import com.jzt.qf.beans.query.GoodsStock_Query;
import com.jzt.qf.dao.OtherBillDao;
import com.jzt.qf.dao.UploadbillJztDao;
import com.jzt.qf.dao.YHJLDao;
import com.jzt.qf.dao.ZxyYhjlJztDao;
import com.jzt.qf.https.JztApi;
import com.jzt.qf.service.OtherBillServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class OtherBillServerImpl implements OtherBillServer {
    @Resource
    private OtherBillDao otherBillDao;
    @Resource
    private JztApi jztApi;
    @Resource
    private UploadbillJztDao uploadbillJztDao;

    @Override
    public HttpResult uoloadLossBill(String BillCode) {
        log.info("上传报损单BuyBillServiceImpl访问成功");
        List<LossBill_Upload> lossBills = otherBillDao.getLossBill(BillCode);
        log.info("上传报损单数据" + JSON.toJSONString(lossBills));
        HttpResult httpResult = jztApi.HttpRequest(
                new UploadBase("stplOrderPlan", "saveData", lossBills));
        log.info("上传报损单返回数据" + JSON.toJSONString(httpResult));
        if (httpResult.isFlag()) {
            for (LossBill_Upload lossBill : lossBills) {
                ZxyUploadbillJzt zxyUploadbillJzt = new ZxyUploadbillJzt();
                zxyUploadbillJzt.setBillcode(lossBill.getBiz_Bill_No());
                zxyUploadbillJzt.setBilltype(9);
                uploadbillJztDao.updateFlags2(zxyUploadbillJzt);
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult uploadOverflowBill(String BillCode) {
        List<LossBill_Upload> lossBills = otherBillDao.getOverflowBill(BillCode);
        log.info("上传报溢单数据" + JSON.toJSONString(lossBills));
        HttpResult httpResult = jztApi.HttpRequest(
                new UploadBase("stplOrderPlan", "saveData", lossBills));
        log.info("上传报溢单数据" + JSON.toJSONString(httpResult));
        if (httpResult.isFlag()) {
            for (LossBill_Upload lossBill : lossBills) {
                ZxyUploadbillJzt zxyUploadbillJzt = new ZxyUploadbillJzt();
                zxyUploadbillJzt.setBillcode(lossBill.getBiz_Bill_No());
                zxyUploadbillJzt.setBilltype(14);
                uploadbillJztDao.updateFlags2(zxyUploadbillJzt);
            }
        }
        return httpResult;
    }
    @Resource
    private ZxyYhjlJztDao zxyYhjlJztDao;
    @Value("${qf.krec}")
    private int krec;
    @Resource
    private YHJLDao yhjlDao;
    @Override
    public void autoYHJL() {
        //Upkeep_Type =  重点商品养护时生成 or 普通商品养护时生成
        //普通养护
        List<ZxyYhjlJzt> ptYHList = zxyYhjlJztDao.getZxyYhjlByUpkeep_Type("普通商品养护时生成");
        if (CollUtil.isNotEmpty(ptYHList)) {
            for (int i = 0; i < ptYHList.size(); i++) {
                int billid = 0;
                ZxyYhjlJzt zxyYhjlJzt = ptYHList.get(i);
                if (i == 0) {
                    Z_InsertMaintainIndex zInsertMaintainIndex = new Z_InsertMaintainIndex();
                    zInsertMaintainIndex
                            .setBillDate(zxyYhjlJzt.getMaintenance_Cate())
                            .setBillCode(zxyYhjlJzt.getBusinessbill_No() + i)
                            .setKRec(krec)
                            .setYHType(0)
                            .setERec(1)
                            .setMaintainUser(zxyYhjlJzt.getMaintenance_Staff())
                            .setChecked(1)
                            .setChecker(0)
                            .setCheckDate(LocalDate.now().toString())
                            .setPlanBillID(0)
                            .setComment("根据三方(九州通)养护记录自动生成");
                    billid = yhjlDao.z_InsertMaintainIndex(zInsertMaintainIndex);
                }
                GoodsStock_Query query = new GoodsStock_Query();
                query.setPuserCode(zxyYhjlJzt.getGoods_No())
                        .setJobnumber(zxyYhjlJzt.getLot_No())
                        .setOutFactoryDate(zxyYhjlJzt.getProduction_Date())
                        .setKrec(krec);
                List<Z_InsertMaintainBill> maintainBillList = yhjlDao.getMaintainBillList(query);
                for (int i1 = 0; i1 < maintainBillList.size(); i1++) {
                    Z_InsertMaintainBill zInsertMaintainBill = maintainBillList.get(i);
                    zInsertMaintainBill
                            .setBillID(billid)
                            .setOrd(i1 + 1)
                            .setMeasures("普通养护")
                            .setStopSale(zxyYhjlJzt.getQuality_State().equals("合格") ? 0 : 1)
                            .setSuggestion(zxyYhjlJzt.getHandling_Suggestion())
                            .setQualitystatus(zxyYhjlJzt.getQuality_State())
                            .setLastmaintainTime(zxyYhjlJzt.getMaintenance_Cate());
                    yhjlDao.z_InsertMaintainBill(zInsertMaintainBill);
                    zxyYhjlJztDao.updateZxyYhjlJztFlag(zxyYhjlJzt.getId());
                }

            }
        }
        //重点养护
        List<ZxyYhjlJzt> zdYHList = zxyYhjlJztDao.getZxyYhjlByUpkeep_Type("重点商品养护时生成");
        if (CollUtil.isNotEmpty(zdYHList)) {
            for (int i = 0; i < zdYHList.size(); i++) {
                int billid = 0;
                ZxyYhjlJzt zxyYhjlJzt = zdYHList.get(i);
                if (i == 0) {
                    Z_InsertMaintainIndex zInsertMaintainIndex = new Z_InsertMaintainIndex();
                    zInsertMaintainIndex
                            .setBillDate(zxyYhjlJzt.getMaintenance_Cate())
                            .setBillCode(zxyYhjlJzt.getBusinessbill_No() + i)
                            .setKRec(krec)
                            .setYHType(1)
                            .setERec(1)
                            .setMaintainUser(zxyYhjlJzt.getMaintenance_Staff())
                            .setChecked(1)
                            .setChecker(0)
                            .setCheckDate(LocalDate.now().toString())
                            .setPlanBillID(0)
                            .setComment("根据三方(九州通)养护记录自动生成");
                    billid = yhjlDao.z_InsertMaintainIndex(zInsertMaintainIndex);
                }
                GoodsStock_Query query = new GoodsStock_Query();
                query.setPuserCode(zxyYhjlJzt.getGoods_No())
                        .setJobnumber(zxyYhjlJzt.getLot_No())
                        .setOutFactoryDate(zxyYhjlJzt.getProduction_Date())
                        .setKrec(krec);
                List<Z_InsertMaintainBill> maintainBillList = yhjlDao.getMaintainBillList(query);
                for (int i1 = 0; i1 < maintainBillList.size(); i1++) {
                    Z_InsertMaintainBill zInsertMaintainBill = maintainBillList.get(i);
                    zInsertMaintainBill
                            .setBillID(billid)
                            .setOrd(i1 + 1)
                            .setMeasures("重点养护")
                            .setStopSale(zxyYhjlJzt.getQuality_State().equals("合格") ? 0 : 1)
                            .setSuggestion(zxyYhjlJzt.getHandling_Suggestion())
                            .setQualitystatus(zxyYhjlJzt.getQuality_State())
                            .setLastmaintainTime(zxyYhjlJzt.getMaintenance_Cate());
                    yhjlDao.z_InsertMaintainBill(zInsertMaintainBill);
                    zxyYhjlJztDao.updateZxyYhjlJztFlag(zxyYhjlJzt.getId());
                }

            }
        }
    }
}
